<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Column extends Model
{
    use HasFactory;

    protected $appends = [
        'status_text','shop_type_text'
    ];

    protected $fillable = ['title','cover','brief','price','original_price','sort','clicks','level','status','shop_type','password','content'];

    const POSTONLY = ['title','cover','brief','price','original_price','sort','clicks','level','status','shop_type','password','content'];

    protected function serializeDate(\DateTimeInterface $date)
    {
        return $date->format($this->dateFormat ?: 'Y-m-d H:i:s');
    }

    public function getStatusTextAttribute()
    {
        return $this->status == 1 ? '上架' : '下架';
    }

    public function getShopTypeTextAttribute()
    {
        $arr = [1=>'免费',2=>'付费',3=>'加密',4=>'指定学员'];
        return $arr[$this->shop_type];
    }

    public function courses()
    {
        return $this->hasMany(ColumnCourse::class,'column_id','id')->with('course');
    }
}
